package cn.signalpha.modules.shop.controller.seller;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.dto.SellerVerifyDTO;
import cn.signalpha.modules.shop.entity.SellerVerify;
import cn.signalpha.modules.shop.service.SellerVerifyService;
import cn.signalpha.modules.shop.vo.SellerVerifyVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/sys-seller/seller/verify")
@Api(value = "商户核销员", tags = "商户核销员")
public class SpiSellerVerifyController extends BaseController {

	private final UserService userService;
	private final SellerVerifyService sellerVerifyService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<SellerVerifyVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("sellerId", getUser().getTenantId());
        return R.data(sellerVerifyService.selectSellerVerifyPage(Condition.getPage(query), params));
    }

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userId", dataType = "string", value = "用户id"),
	})
	@ApiOperation(value = "分页", notes = "")
	public R<List<SellerVerifyVO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		params.put("sellerId", getUser().getTenantId());
		return R.data(sellerVerifyService.selectSellerVerifyList(params));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<SellerVerify> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(sellerVerifyService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入sellerVerify")
	public R save(@Valid @RequestBody SellerVerifyDTO sellerVerifyDTO) {
		Long sellerId = Long.valueOf(getUser().getTenantId());
		User user = userService.getByMobile(sellerVerifyDTO.getMobile());
		Assert.isNull(user, "该手机号未查询到用户");
		if(sellerVerifyService.countBySellerIdAndUserId(sellerId, user.getId()) > 0){
			throw new ServiceException("请不要重复添加");
		}

		SellerVerify sellerVerify = new SellerVerify();
		sellerVerify.setSellerId(sellerId);
		sellerVerify.setUserId(user.getId());
		sellerVerify.setCreateTime(new Date());
		sellerVerifyService.save(sellerVerify);
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sellerVerifyService.removeByIds(Func.toLongList(ids)));
	}

}
